﻿using System;
using System.Data;
using Model.Entity;

namespace BLL
{
    public class RenewBLL : BaseBLL
    {
        public UserAndBorrow getBorrowsByUser(int userId)
        {
            return new ReturnBLL().getBorrowsByUser(userId);
        }

        public Borrow renewBook(String rfid)
        {
            Borrow borrow = dal.getBorrowDAL().getByRfidWithoutReturn(rfid);
            if (borrow != null && borrow.renewCount < borrow.user.userType.enableRenewCount && borrow.shouldReturnDate >= DateTime.Now)
            {
                ++borrow.renewCount;
                borrow.shouldReturnDate = DateTime.Now.AddDays(borrow.user.userType.enableBorrowDay);
                dal.getBorrowDAL().update(borrow);
                return borrow;
            }
            else
            {
                return null;
            }
        }

        public int renewAllByUser(User user)
        {
            return dal.getBorrowDAL().renewAllByUser(user.id, DateTime.Now.AddDays(user.userType.enableBorrowDay));
        }
    }
}
